<?php

if (!defined('IN_D')) die("Error 404");



function admin_viewpages($ttrow,$n,$pg){

	global $link;

	$link = preg_replace("#&pg=([0-9]{1,})#si","",$link);

	$html="<table width=100% valign=bottom cellpadding=2 cellspacing=2>";

	$html.="<tr><td align=center>";

	$pgt = $pg-1;

	if ($pg<>1) $html.="<a class=pagelink href=$link onfocus=this.blur() title ='Xem trang đầu'><b>&laquo;&laquo;</b></a> <a class=pagelink href=$link&pg=$pgt onfocus=this.blur() title='Xem trang trước'><b>&laquo;</b></a> ";

	for($l = 0; $l < $ttrow/$n; $l++) {

		$m = $l+1;

		if($m == $pg) $html .= "<a onfocus=this.blur() class=pagecurrent>$m</a> ";

		else $html .= "<a onfocus=this.blur() href=$link&pg=$m title='Xem trang $m' class=pagelink>$m</a> ";

	}

	$pgs = $pg+1;

	if ($pg<>$m) $html.="<a class=pagelink href=$link&pg=$pgs onfocus=this.blur() title='Xem trang kế tiếp'><b>&raquo;</b></a> <a class=pagelink href=$link&pg=$m onfocus=this.blur() title='Xem trang cuối'><b>&raquo;&raquo;</b></a> ";

	$html.="</td></tr></table>";

	return $html;

}

function acp_quick_add_singer_form($j='') {
if($j){
	$html = "<input name=new_singer[".$j."] size=50> &nbsp; <select name=singer_type[".$j."] id=singer_type[".$j."]>";
	$html.="<option value=1 selected>Ca sỹ & Ban nhạc VN</option>".
		"<option value=2>Ca sỹ & Ban nhạc QT</option>".
	"</select>";
	}
else{
		$html = "<input name=new_singer size=50>&nbsp; IMG <input name=new_singer_img size=30> &nbsp; <select name=singer_type id=singer_type onchange=\"changeallselect('singer_type');\">";
		$html.="<option value=1 selected>Ca sỹ & Ban nhạc VN</option>".
		"<option value=2>Ca sỹ & Ban nhạc QT</option>".
	"</select>";
	}
	return $html;
}
function acp_quick_add_singer($new_singer,$singer_type,$singer_img='') {
	global $DB,$conf,$func;
	$new_singer = $func->htmlchars(stripslashes($new_singer));
	$singer_img = $func->htmlchars(stripslashes($singer_img));
	$q = $DB->query("SELECT singer_id FROM ".$conf['prefix']."singer WHERE singer_name = '".$new_singer."'");
	if ($DB->num_rows($q)) {
		$r = $DB->fetch_row($q);
		$singer = $r[0];
	}
	else {
		$DB->query("INSERT INTO ".$conf['prefix']."singer (singer_name,singer_name_ascii,singer_type,singer_local_img) VALUES ('".$new_singer."','".strtolower($func->utf8_to_ascii($new_singer))."','".$singer_type."','".$singer_img."')");
		$singer = $DB->insert_id();
	}
	return $singer;
}

function acp_quick_add_album($new_album,$album_cat,$album_img='',$album_singer) {
	global $DB,$conf,$func;
	$new_album=$func->htmlchars(stripslashes($new_album));
	$album_img=$func->htmlchars(stripslashes($album_img));
	$q = $DB->query("SELECT mutli_id FROM ".$conf['prefix']."multimedia WHERE multi_name = '".$new_album."' AND multi_type=1");
	if ($DB->num_rows($q)) {
		$r = $DB->fetch_row($q);
		$album = $r[0];
	}
	else {
		$DB->query("INSERT INTO ".$conf['prefix']."multimedia (multi_name,multi_name_ascii,multi_cat,multi_local_img,multi_singer,multi_type) VALUES ('".$new_album."','".strtolower($func->utf8_to_ascii($new_album))."','".$album_cat."','".$album_img."','".$album_singer."','1')");
		$album = $DB->insert_id();
	}
	return $album;
}
function acp_multi_list($id = 0, $add = false,$j='',$type='') {
	global $DB,$conf,$lang_acp,$func;
	if($type=='album')
	$q = $DB->query("SELECT * FROM ".$conf['prefix']."multimedia WHERE multi_type=1 ORDER BY multi_name ASC");
	elseif($type=='movie')
	$q = $DB->query("SELECT * FROM ".$conf['prefix']."multimedia WHERE multi_type=2 ORDER BY multi_name ASC");
if($j)
	$html = "<select name=multimedia[".$j."] id=multimedia[".$j."]>";
else
	$html = "<select name=multimedia id=multimedia onchange=\"changeallselect('multimedia');\">";
	if ($add) $html .= "<option value=dont_edit".(($id == 0)?" selected":'').">{$lang_acp['no_edit']}</option>";
	if($type=='album')
		$html .= "<option value=0".(($id == 0 && !$add)?" selected":'').">{$lang_acp['unknown']}</option>";

	while ($r = $DB->fetch_row($q)) {

		$html .= "<option value=".$r['multi_id'].(($id == $r['multi_id'])?" selected":'').">".$func->getwords($r['multi_name'],10)."</option>";

	}

	$html .= "</select>";

	return $html;

}




function acp_singer($id = 0, $add = false,$j='') {

	global $DB,$conf;

	$id = (int)$id;

	$q = $DB->query("SELECT * FROM ".$conf['prefix']."singer ORDER BY singer_name ASC");
if($j)
	$html = "<select name=singer[".$j."] id=singer[".$j."]>";
else
	$html = "<select name=singer id=singer onchange=\"changeallselect('singer');\">";

	if ($add) $html .= "<option value=dont_edit".(($id == 0)?" selected":'').">Không sửa</option>";

	$html .= "<option value=-1".((($id == -1 || $id == 0) && !$add)?" selected":'').">Chưa biết (VN)</option>";

	$html .= "<option value=-2".(($id == -2 && !$add)?" selected":'').">Chưa biết (QT)</option>";

	//$html .= "<option value=-3".(($id == -3 && !$add)?" selected":'').">Chưa biết (Band)</option>";

	while ($r = $DB->fetch_row($q)) {

		$html .= "<option value=".$r['singer_id'].(($id == $r['singer_id'])?" selected":'').">".$r['singer_name']."</option>";

	}

	$html .= "</select>";

	return $html;

}

function acp_singer_type($i) {
	global $lang_acp;
	$html = "<select name=singer_type>".
		"<option value=1".(($i==1)?' selected':'').">{$lang_acp['indigenous_singer']}</option>".
		"<option value=2".(($i==2)?' selected':'').">{$lang_acp['foreign_singer']}</option>".
		"<option value=9".(($i==9)?' selected':'').">{$lang_acp['member_show']}</option>".
	"</select>";
	return $html;

}

function acp_multimedia_type($i) {
	global $lang_acp;
	$html="<form name=\"fr_multimedia_type\" method=\"post\" action=\"index.php\"><select name=\"multimedia_type\" id=multimedia_type onchange=\"this.form.submit();\">
				<option >{$lang_acp['select_multimedia_type']}</option>
				<option value=\"1\" >-- {$lang_acp['album']}</option>
				<option value=\"2\" >-- {$lang_acp['movie']}</option>
				</select></form>";
	return $html;
}

function acp_cat($id = 0, $add = false,$j='') {
	global $DB,$conf,$lang_acp;
	$q = $DB->query("SELECT * FROM ".$conf['prefix']."cat ORDER BY sub_id ASC");
	while ($r = $DB->fetch_row($q)) {
		if (!$r['sub_id']) $arr[$r['cat_id']][0] = $r['cat_name'];
		elseif ($arr[$r['sub_id']][0]) $arr[$r['sub_id']][] = array($r['cat_id'],$r['cat_name']);
	}
if($j)
	$html = "<select name=cat[".$j."] id=cat[".$j."] >";
else
	$html = "<select name=cat id=cat onchange=\"changeallselect('cat');\">";
	if ($add) $html .= "<option value=dont_edit".(($id == 0)?" selected":'').">{$lang_acp['no_edit']}</option>";
	if ($arr) 
		foreach ($arr as $key => $val) {
			$html .= "<optgroup label='".$val[0]."'>";
			for ($i=1;$i<count($val);$i++) {
				$html .= "<option value=".$val[$i][0].(($id == $val[$i][0])?" selected":'').">".$val[$i][1]."</option>";
			}
			$html .= "</optgroup>";
		}
	$html .= "</select>";
	return $html;
}



function acp_user_level($lv) {
	$html = "<select name=level>".
		"<option value=1".(($lv==1)?' selected':'').">Admin</option>".
		"<option value=2".(($lv==2)?' selected':'').">Moderator</option>".
		"<option value=3".(($lv==3)?' selected':'').">Member</option>".
	"</select>";
	return $html;

}
function acp_user_sex($s) {
	$html = "<select name=sex>".
		"<option value=1".(($s==1)?' selected':'').">Nam</option>".
		"<option value=2".(($s==2)?' selected':'').">Nữ</option>".
	"</select>";
	return $html;
}
function acp_cat_type($id) {
	$html = "<select name=cat_types>".
		"<option value=1".(($id==1)?' selected':'').">Media</option>".
		"<option value=2".(($id==2)?' selected':'').">Movie</option>".
	"</select>";
	return $html;
}
function acp_maincat($id) {
	global $conf,$DB,$lang_acp;
	$q = $DB->query("SELECT * FROM ".$conf['prefix']."cat WHERE sub_id = 0 OR sub_id IS NULL ORDER BY cat_order ASC");
	$html = "<select name=sub>";
	$html .= "<option value=0>- {$lang_acp['root_cat']} -</option>";
	while ($r = $DB->fetch_row($q)) {
		$html .= "<option value=".$r['cat_id'].(($r['cat_id'] == $id)?" selected":"").">".$r['cat_name']."</option>";
	}
	$html .= "</select>";
	return $html;
}
function acp_news_maincat($id) {
	global $DB,$conf;
	$q = $DB->query("SELECT * FROM ".$conf['prefix']."news_cat WHERE sub_id = 0 OR sub_id IS NULL ORDER BY cat_order ASC");
	$html = "<select name=sub>";
	$html .= "<option value=0>- Mục chính -</option>";
	while ($r = $DB->fetch_row($q)) {
		$html .= "<option value=".$r['cat_id'].(($r['cat_id'] == $id)?" selected":"").">".$r['cat_name']."</option>";
	}
	$html .= "</select>";
	return $html;
}

function acp_get_mod_permission() {
	global $DB,$conf;
	$permission_list = array(
		'add_cat',		'edit_cat',		'del_cat',
		'add_media',	'edit_media',	'del_media',
		'add_singer',	'edit_singer',	'del_singer',
		'add_multimedia',	'edit_multimedia',	'del_multimedia',
		'add_user',		'edit_user',	'del_user',
		'add_link',		'edit_link',	'del_link',
		'add_template',	'edit_template','del_template',
		'edit_request',
	);
	$per = $conf['mod_permission'];
	$per = decbin($per);
	$len = count($permission_list);
	while (strlen($per) < $len) $per = '0'.$per;
	for ($i=0;$i<$len;$i++) $arr[$permission_list[$i]] = $per[$i];
	return $arr;
}



function acp_check_permission($t) {
	global $level,$mod_permission;
	if ($level == 2 && !$mod_permission[$t]) die('<center>'.$lang_acp['u_not_enter'].' !</center>');
}



function acp_server($type='',$j='') {
	global $conf;
	if(!$j)
    $html = "<select name=local_url id=local_url onchange=\"changeallselect('local_url');\">";
	else
	$html = "<select name=local_url[{$j}] id=local_url[{$j}]>";
	$html.="<option value=0>- Server Local -</option>";
	preg_match_all("#<server>(.*?)</server>#",$conf['data_server'],$r);
	if(count($r[0])>0){
		for($i=0;$i<=count($r[0])-1;$i++){
			preg_match("#<id>([0-9]+)</id>#",$r[1][$i],$match_id);
				$html.="<option value={$match_id[1]}".(($type==$match_id[1])?' selected':'').">Server {$match_id[1]}</option>";
		}
	}
        $html .= "</select>";
    return $html;
}  

function acp_lserver($type='',$j='') {
	global $conf;
	if(!$j)
    $html = "<select name=link_server id=link_server onchange=\"changeallselect('link_server');\">";
	else
	$html = "<select name=link_server[{$j}] id=link_server[{$j}]>";
	$html.="<option value=0>- Link Server -</option>";
	preg_match_all("#<lserver>(.*?)</lserver>#",$conf['data_lserver'],$r);
	if(count($r[0])>0){
		for($i=0;$i<=count($r[0])-1;$i++){
			preg_match("#<name>(.*?)</name>#",$r[1][$i],$match_name);
			preg_match("#<id>([0-9]+)</id>#",$r[1][$i],$match_id);
        	$html.="<option value={$match_id[1]}".(($type==$match_id[1])?' selected':'').">{$match_name[1]}</option>";
		}
	}
        $html .= "</select>";
    return $html;
}  


function acp_type($url){
global $DB,$conf,$func;
$result=false;
$par=$func->get_ext($url);
if (in_array($par,array('flv','wmv','mp3','avi','divx','mp4','mpeg'))){
	$ext='ext:'.$par;
	$query_ext = $DB->query("SELECT * FROM ".$conf['prefix']."player where player_check ='$ext'");
	$count_ext=$DB->num_rows($query_ext);
	if($count_ext){
	$kq=$DB->fetch_row($query_ext);
	$result=$kq['player_id'];
	}
}
else{

	preg_match('@^(?:http://)?([^/]+)@i',$url, $matches);
	$site = 'site:'.$matches[1];
	$query_site = $DB->query("SELECT * FROM ".$conf['prefix']."player where player_check ='$site'");
	$count_site=$DB->num_rows($query_site);
	if($count_site){
		$kq=$DB->fetch_row($query_site);
		$result=$kq['player_id'];
	}
}
return $result;
}

function leech_img($url,$type){
global $DB,$conf,$func;
	if($type=="album"){
		$w=200;
		$h=200;
		$w_t=150;
		$h_t=150;
		$thumb_dir=$conf['album_thumb'];
		$dir=$conf['album_img'];
	}
	elseif($type=="movie"){
		$w=200;
		$h=300;
		$w_t=150;
		$h_t=200;
		$thumb_dir=$conf['img_thumb'];
		$dir=$conf['root_img'];
	}
	elseif($type=="singer"){
		$w=150;
		$h=150;
		$dir=$conf['singer_img'];
	}
	elseif($type=="focus_multi"){
		$w=500;
		$h=250;
		$dir=$conf['focus_img'];
		$w_t=70;
		$h_t=50;
		$thumb_dir=$conf['focus_thumb'];
	}
	if(!file_exists("cook.txt")){
		$fpt = fopen("cook.txt", "w");
		fwrite($fpt, "");
		fclose($fpt); 
		chmod("cook.txt",0777);
	}
	 $ch = curl_init(); 
	 curl_setopt ($ch, CURLOPT_URL, $url); 
	 curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com");
	 curl_setopt ($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
	 curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
	 curl_setopt ($ch, CURLOPT_COOKIEJAR, "cook.txt");
	 curl_setopt($ch, CURLOPT_COOKIEFILE, "cook.txt");
	 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
	 curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
	 curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
	 $result = curl_exec ($ch);
	 curl_close($ch);
	 $FileName = explode("/", $url);
	 $FileName = $FileName[count($FileName)-1];

			$rename = time();

	$ext=$func->get_ext($FileName);
	$save_file='cache/'.$rename.".".$ext;
		$fp = fopen($save_file, "w");
	fwrite($fp, $result,strlen($result));
	fclose($fp);
	$kq=$rename.".".$ext;
	$func->img_resize( $save_file ,$w,$h,"../".$dir ,$kq);
	if($thumb_dir){
	$func->img_resize( $save_file ,$w_t,$h_t,"../".$thumb_dir, $kq);
	}
	unlink($save_file);
	/*
	$rename = time();
	$link=$conf['local_img'].'thumb.php?url='.urlencode($url).'&name='.$rename;
	$kq=$this->get_by_curl_not_header($link);
	*/
	return $kq;
}

function acp_focus($focus,$type){
	global $lang_acp;
	switch($type){
		case "album" : 
			switch($focus){
				case "1" : $ro=$lang_acp['domestic'];break;
				case "2" : $ro=$lang_acp['foreign'];break;
			}			
			break;
		case "movie" :
			switch($focus){
				case "3" : $ro=$lang_acp['hot'];break;
				case "4" : $ro=$lang_acp['projecting_cinema'];break;
			}			
			break;
		case "song" :
			switch($focus){
				case "5" : $ro=$lang_acp['hot'];break;
				case "6" : $ro=$lang_acp['top_mtv'];break;
			}			
			break;
	}
	return $ro;
}

function acp_select_focus($type){
	global $lang_acp;
	$ro="<select name='type_focus'>";
	switch($type){
		case "album" : 
			$ro.="<option value=1>".$lang_acp['domestic']."</option>";
			$ro.="<option value=2>".$lang_acp['foreign']."</option>";
				break;
		case "movie" :
				$ro.="<option value=3>".$lang_acp['hot']."</option>";
				$ro.="<option value=4>".$lang_acp['projecting_cinema']."</option>";			
			break;
		case "song" :
				$ro.="<option value=5>".$lang_acp['hot']."</option>";
				$ro.="<option value=6>".$lang_acp['top_mtv']."</option>";		
			break;
	}
	$ro.="</select>";
	return $ro;
}
?>